using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.Bll
{
    public abstract class BaseCollection<T> : List<T> where T : BaseItem, new()
    {
        private List<T> _deleted;
        protected List<T> Deleted
        {
            get
            {
                if (_deleted == null)
                {
                    _deleted = new List<T>();
                }
                return _deleted;
            }
        }

        public void SaveChanges()
        {
            foreach (T item in this)
            {
                item.SaveChanges();
            }
            if (_deleted != null)
            {
                foreach (T item in Deleted)
                {
                    item.SaveChanges();
                }
                _deleted.Clear();
            }
        }

        public T Add()
        {
            T tmp = new T();
            this.Add(tmp);
            return tmp;
        }

        public void Delete(T item)
        {
            foreach (T i in this)
            {
                if (i == item)
                {
                    Deleted.Add(i);
                    item.Delete();
                    this.Remove(i);
                    return;
                }
            }
        }
    }
}
